package com.heo.web.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.heo.web.entity.Device;
import com.heo.web.entity.KaoLaDeviceInfo;
import com.heo.web.service.HttpDeviceService;
import com.heo.web.util.GsonUtils;
import com.heo.web.util.RedisUtil;
import com.heo.web.util.Utils;


@Service
@Transactional(readOnly = true)
public class HttpDeviceServiceImpl implements HttpDeviceService {
	
	@Autowired
	private RedisUtil redisUtill;


	public Map<String, Object> deviceController(Device device) throws Exception {
		Map<String, Object> rspJson = new HashMap<String, Object>();
		// 设备状态，0：低电频；1：高电频3:表示已经发送脉冲
		int status = device.getS();
		String macAddress = device.getM();
		int vs = Utils.getInt(device.getV()); // 版本号参数
//		Map<String, Object> washerInfo = washDao.getWasherInfo(macAddress);
		KaoLaDeviceInfo kaoLaDeviceInfo = null;
		String jsonString =  (String) redisUtill.getValue(macAddress);
		 kaoLaDeviceInfo = (KaoLaDeviceInfo) GsonUtils.jsonToObject(jsonString, KaoLaDeviceInfo.class);
		if (kaoLaDeviceInfo == null) {
			return paramsMap(rspJson, -2, 600);
		}
		
		//Long deviceStatus = Utils.getLong(washerInfo.get("WASHER_STATUS"));
		// 修改设备iccid号码
		String iccid = device.getI();
		
		if (Utils.checkNull(iccid)) {
			/*long washerId = Utils.getLong(washerInfo.get("WASHER_ID"));
			//updateWasherElectricCodeId(washerId, iccid);
			//初始化洗衣工作
			Map<String, Object> washingInfos = washDao.getWashingInfo(macAddress);
			if(washingInfos != null && washingInfos.size() > 0){
				int finishStatus = Utils.getInt(washingInfos.get("FINISH_STATUS"));
				if(finishStatus == 3){
					long washingIds = Utils.getLong(washingInfos.get("WASHING_ID"));
					// 如果设备状态在工作中，那么改变状态为 空闲
					//updateDeviceStatus(macAddress,Constants.WASHDEVICE_STATUS_ZORE, washingIds,Constants.WASH_STATUS_ZORE);
				}
			} else {
				//updateWashStatus(macAddress, Constants.WASHDEVICE_STATUS_ZORE);
			}*/
			
			
			/**
			 * d参数
			 * CCCCCP CCCCC：脉冲( 00050=表示50ms脉冲，00100=表示100ms脉冲)
			 * P：电路接口模式，0=表示通用接口，1=特殊洗衣机接口 000500：表示50ms脉冲模式，0表示http 1表示tcp
			 * 表示投币模式,0表示硬件接口模式,1表示通讯接口模式
			 */
			int deviceType = Utils.getInt(kaoLaDeviceInfo.getDeviceType());
//			if (deviceType == 1) {
//				rspJson.put("d", "0004501");
//			} else {
//				rspJson.put("d", "0004500");
//			}
			
			int pulseWidth = Utils.getInt(kaoLaDeviceInfo.getPulseWidth());
			StringBuilder dSb = new StringBuilder("00");
			if(pulseWidth > 0){
				if(pulseWidth < 100){
					dSb.append("0").append(pulseWidth);
				}else{
					dSb.append(pulseWidth);
				}
			}else{
				dSb.append("045");
			}
			
			rspJson.put("l", 180);
			if(vs > 1){
				if (deviceType == 1) {
					dSb.append("11");
				} else {
					dSb.append("10");
				}
				rspJson.put("d", dSb.toString());
				rspJson.put("t", 10);
//				rspJson.put("s", dockingSetMap.get("NEXT_REQ_IP")); // 
//				rspJson.put("p", dockingSetMap.get("NEXT_SOCKET_PORT")); // Utils.getInteger()
//				rspJson.put("s", "43.247.68.131");// rspJson.put("s", "218.16.123.124");
//				rspJson.put("p", "8090");
				//int proxyXz = Utils.getInt(washerInfo.get("PROXY_XIAOZHANG"));
				//if(proxyXz == 1){
					String ip = Utils.getStr(kaoLaDeviceInfo.getProxyIp());
					String port = Utils.getStr(kaoLaDeviceInfo.getProxyPort());
					if(Utils.checkNull(ip) && Utils.checkNull(port)) {
						rspJson.put("s", ip); 
						rspJson.put("p", port); 
					}
				//}
				
			}
			
			if (deviceType == 1) {
				dSb.append("11");
			} else {
				dSb.append("10");
			}
			rspJson.put("d", dSb.toString());
		}
		
		return rspJson;

	}

	private Map<String, Object> paramsMap(Map<String, Object> rspJson, int cmd,
			int time) {
		//Map<String, Object> dockingSetMap = cache.getDockingSet();
		if (cmd != -2) {
			rspJson.put("c", cmd);
		}

		String macAddress = Utils.getStr(rspJson.get("m"));
		//String macCmd = cache.getMacAddress(macAddress);
		/*if (Utils.checkNull(macCmd)) {
			int macCmds = Utils.getInteger(macCmd);
			if (macCmds != -1) {
				rspJson.put("g", macCmds);
			}
			rspJson.put("c", macCmds);
		}*/
		rspJson.put("t", time);
//		rspJson.put("s", dockingSetMap.get("NEXT_REQ_IP")); // 
//		rspJson.put("p", dockingSetMap.get("NEXT_REQ_PORT")); // Utils.getInteger()
		rspJson.put("s", "8zao.net");
		rspJson.put("p", "9999");
		// rspJson.put("u", "/d/h");
		//rspJson.put("u", dockingSetMap.get("NEXT_REQ_PATH")); // 
		//rspJson.put("i", dockingSetMap.get("HTTP_TIME_OUT"));
		return rspJson;
	}
	
}

